package org.algorithm.wE1.链表;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * good luck guys
 * keep learing for algorithm
 *
 */
public class 两数之和 {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,6,7,8,9};
        System.out.println(Arrays.toString(sum(arr,7)));
    }
    public static int[] sum(int[] arr,int sum){
        if(arr == null || arr.length<2){
            throw new IllegalArgumentException("arr.length<0");
        }
        //Map
        Map<Integer,Integer> map = new HashMap<>(2);
        for (int i = 0; i <arr.length ; i++) {
            if(map.containsKey(sum-arr[i])){
                return new int[]{map.get(sum-arr[i]),i};
            }
            map.put(arr[i] ,i);
        }
        return null;
    }
}
